/*
============================================================================
Name        : PlayerDevAppUi.cpp
Author      : Jian
Version     :
Copyright   : GPL
Description : Main application UI class (controller)
============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <PlayerDev.rsg>
#include <f32file.h>
#include <s32file.h>

#include "PlayerDev.pan"
#include "PlayerDevAppUi.h"
#include "PlayerDevAppView.h"
#include "PlayerDev.hrh"

#include "dd_test.h"
#include "dd_player.h"
#include "dd_log.h"
#include "dd_http.h"
#include "sd_platform.h"
#include "dd_util.h"
#include "dd_zip.h"
#include "dd_ini_reader.h"
#include "dd_http_worker.h"
#include "dd_http_test.h"


// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CPlayerDevAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//

static TInt _threadFunction(TAny* aPtr)
{
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    __DD_ASSERT_DEFENSE(cleanupStack != NULL, return KErrNoMemory);

    CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler;
    __DD_ASSERT_DEFENSE(activeScheduler != NULL, return KErrNoMemory);

    CActiveScheduler::Install(activeScheduler);
    
    dd_env_init() ;

    dd_player_run() ;

    // If we get here, then that means we are ready to start our
    // own active scheduler in this thread.
    // From this point on, the CActiveScheduler::Start() doesn't
    // return until we call CActiveScheduler::Stop().
    //
    // NOTE: active scheduler MUST always have at least one
    // outstanding active object request. Otherwise, active
    // scheduler will hang in the WaitForAnyRequest() method
    // and never handle the call to ActiveScheduler::Stop().
    CActiveScheduler::Start();

    // If we get here, then that means we called CActiveScheduler::Stop()
    // and thread is ready to die.
    delete activeScheduler;
    delete cleanupStack;

    return KErrNone;
}



void CPlayerDevAppUi::ConstructL()
{
    // Initialise app UI with standard value.
    BaseConstructL();

    // Create view object
    iAppView = CPlayerDevAppView::NewL( ClientRect() );
    
    RThread playerThread ;
    TInt err = playerThread.Create( 
        _L("player_thread"), _threadFunction,
        0x8000, NULL, 
        NULL);

    playerThread.Resume();
}
// -----------------------------------------------------------------------------
// CPlayerDevAppUi::CPlayerDevAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CPlayerDevAppUi::CPlayerDevAppUi()
{
    // No implementation required
}

// -----------------------------------------------------------------------------
// CPlayerDevAppUi::~CPlayerDevAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CPlayerDevAppUi::~CPlayerDevAppUi()
{
    if ( iAppView )
    {
        delete iAppView;
        iAppView = NULL;
    }

}

// -----------------------------------------------------------------------------
// CPlayerDevAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//

static void showDialog(const TDesC& str) 
{
    CAknInformationNote* informationNote = new (ELeave)CAknInformationNote(ETrue);
    informationNote->ExecuteLD(str);
}

static int _test_process_body(const dd_http* http, const char* data, int offset, int len) 
{
    sd_file* file = (sd_file*)http->callback.clientPtr ;

    sd_file_seek(file, offset, DD_SEEK_SET) ;
    sd_file_write(file, data, len) ;

    return 0 ;
}

void CPlayerDevAppUi::HandleCommandL( TInt aCommand )
{
    switch( aCommand )
    {
    case EEikCmdExit:
    case EAknSoftkeyExit:
        Exit();
        break;
    case EPlayerDevCommand1:
        {
            showDialog(dd_http_test() ? _L("SUCCEED"): _L("FAILED")) ;

            /*
            showDialog(dd_test() ? _L("SUCCEED"): _L("FAILED")) ;
            dd_player_play("file:// 1") ;
            */
            /*
            const char* reqType = dd_ini_read_key("testHttpType") ;
            const char* url = dd_ini_read_key("testHttpUrl") ;
        
            if (reqType && dd_strcasecmp(reqType, "GET") == 0) {
                dd_http_req_get req = {0} ;
                req.callback.process_body = _test_process_body ;
                req.callback.clientPtr = sd_file_open((const DD_UTF8*)"c:\\data\\test.html", DD_FILE_REPLACE) ;
                req.url = (DD_UTF8*)url ;
                req.port = 80 ;
                dd_http_get(&req) ; 
            } else {
                dd_http_req_head req = {0} ;
                req.callback.process_body = _test_process_body ;
                req.callback.clientPtr = sd_file_open((const DD_UTF8*)"c:\\data\\test.html", DD_FILE_REPLACE) ;
                req.url = (DD_UTF8*)url ;
                req.port = 80 ;

                dd_http_head(&req) ;
            }
            */                

        }
        break;
    default:
        Panic( EPlayerDevUi );
        break;
    }
}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CPlayerDevAppUi::HandleStatusPaneSizeChange()
{
    iAppView->SetRect( ClientRect() );

} 


// End of File
